package com.hyfrogx.modules.job.utils;

import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.modules.job.entity.HyAdJobEntity;
import org.quartz.*;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/10/18 14:47
 * @Company 汉咏科技
 */
public class HyAdJobUtils {

    /**
     * JobMap的参数key
     */
    public static final String JOB_PARAM_KEY = "JOB_PARAM_KEY";

    /**任务前缀
     *
     */
    private final static String ADJOB_NAME = "AdJob_";

    /**
     * 获取触发器key
     */
    public static TriggerKey getTriggerKey(Long jobId) {
        return TriggerKey.triggerKey(ADJOB_NAME + jobId);
    }

    /**
     * 获取jobKey
     */
    public static JobKey getJobKey(Long jobId) {
        return JobKey.jobKey(ADJOB_NAME + jobId);
    }

    /**
     * 获取表达式触发器
     */
    public static CronTrigger getCronTrigger(Scheduler scheduler, Long jobId) {
        try {
            return (CronTrigger) scheduler.getTrigger(getTriggerKey(jobId));
        } catch (SchedulerException e) {
            throw new MyException("获取定时任务CronTrigger出现异常", e);
        }
    }
    /**
     * 创建定时任务
     */
    public static void createScheduleJob(Scheduler scheduler, HyAdJobEntity hyAdJobEntity) {
        try {
            //构建job信息
            JobDetail jobDetail = JobBuilder.newJob(HyAdJob.class).withIdentity(getJobKey(hyAdJobEntity.getJobId())).build();

            //表达式调度构建器
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(hyAdJobEntity.getCronExpression())
                    .withMisfireHandlingInstructionDoNothing();

            //按新的cronExpression表达式构建一个新的trigger
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(hyAdJobEntity.getJobId())).withSchedule(scheduleBuilder).build();

            //放入参数，运行时的方法可以获取
            jobDetail.getJobDataMap().put(JOB_PARAM_KEY, hyAdJobEntity);

            scheduler.scheduleJob(jobDetail, trigger);
            System.out.println("广告定时任务创建成功");
            scheduler.start();

        } catch (SchedulerException e) {
            //TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new MyException("创建定时任务失败", e);
        }
    }
    /**
     * 更新定时任务
     */
    public static void updateScheduleJob(Scheduler scheduler, HyAdJobEntity hyAdJobEntity) {
        try {
            TriggerKey triggerKey = getTriggerKey(hyAdJobEntity.getJobId());

            //表达式调度构建器
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(hyAdJobEntity.getCronExpression())
                    .withMisfireHandlingInstructionDoNothing();

            CronTrigger trigger = getCronTrigger(scheduler, hyAdJobEntity.getJobId());

            //按新的cronExpression表达式重新构建trigger
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();

            //参数
            trigger.getJobDataMap().put(JOB_PARAM_KEY, hyAdJobEntity);
            System.out.println("广告定时任务修改");
            scheduler.rescheduleJob(triggerKey, trigger);


        } catch (SchedulerException e) {
            throw new MyException("更新定时任务失败", e);
        }
    }
    /**
     * 删除定时任务
     */
    public static void deleteScheduleJob(Scheduler scheduler, Long jobId) {
        try {
            scheduler.deleteJob(getJobKey(jobId));
        } catch (SchedulerException e) {
            //TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new MyException("删除定时任务失败", e);
        }
    }
}
